﻿using BLL;
using EncryptHelper;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.登录用户信息
{
    public partial class AddUserInfo : System.Web.UI.Page
    {
        RoleBLL bllrole = new RoleBLL();
        UserBLL man = new UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAaddUser();
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            //BindUserInfo();
            BindRoleAdd();
        }

        #region 添加成员信息.角色列表绑定
        /// <summary>
        /// 绑定角色列表
        /// </summary>
        private void BindAaddUser()
        {
            DataTable table = bllrole.GetRoleList();
            drop_RoleID.DataSource = table;
            drop_RoleID.DataTextField = "RoleName";
            drop_RoleID.DataValueField = "RoleID";
            drop_RoleID.DataBind();

            drop_RoleID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }
        #endregion

        #region 绑定用户信息，添加用户信息
        /// <summary>
        /// 绑定用户信息，添加用户信息
        /// </summary>
        private void BindRoleAdd()
        {
            //接收用户数据的信息
            string name = txt_Name.Text.Trim();
            int sex = Convert.ToInt32(rbl_Sex.SelectedValue);
            string account = txt_Accouont.Text.Trim();
            string pwasd = txt_Pwads.Text;
            string affirm = txt_Affirm.Text;
            string phone = txt_Phone.Text.Trim();
            string email = txt_Email.Text.Trim();
            int roleid = Convert.ToInt32(drop_RoleID.SelectedValue);

            //电话进行格式和非空等数据验证
            if (!Regex.IsMatch(phone, @"^1[3578]\d{9}$"))
            {
                lb_Info.Text = "输入的电话格式不正确！";
                return;
            }
            //邮箱
            if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
            {
                lb_Info.Text = "输入的电子邮箱格式不正确！";
                return;
            }

            //调用MODEL层中的数据库信息
            UserAdd add = new UserAdd()
            {
                //接收用户数据的信息
                UserName = name,
                Sex = Convert.ToInt32(rbl_Sex.SelectedValue),
                Account = account,
                Password = pwasd,
                Phone = phone,
                Email = email,
                FK_RoleID = Convert.ToInt32(drop_RoleID.SelectedValue)
            };

            int result = man.Add(add);
            if (result == 1)
            {
                //跳转页面到用户信息
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');location.href='UserInfo.aspx';</script>");
                JSHelper.Alert(this, "添加成功！", "UserInfo.aspx");
            }
            else if (result == -1)
            {
                lb_Info.Text = "这个名称你已添加过，请勿重复添加！";
            }
            else
            {
                lb_Info.Text = "添加失败";
            }
        } 
        #endregion
    }
}